package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.common.core.constant.SupervisionBusinessConstants;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.supervision.dto.JzdxChangePlaceDTO;
import com.engine4cloud.tdf.supervision.dto.JzdxDieDTO;
import com.engine4cloud.tdf.supervision.dto.JzdxPrisonDTO;
import com.engine4cloud.tdf.supervision.dto.create.JzdxReleaseCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxReleaseResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxReleaseUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxReleaseEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Mapper
public interface JzdxReleaseConvert {
    JzdxReleaseConvert INSTANCE = Mappers.getMapper(JzdxReleaseConvert.class);

    JzdxReleaseEntity createTo(JzdxReleaseCreateDTO s);

    JzdxReleaseEntity updateTo(JzdxReleaseUpdateDTO s);

    JzdxReleaseResultDTO convertTo(JzdxReleaseEntity s);

    JzdxPrisonDTO convertPrison(JzdxReleaseEntity s);

    JzdxDieDTO convertDie(JzdxReleaseEntity s);

    JzdxChangePlaceDTO convertChangePlaceTo(JzdxReleaseEntity s);

    default JzdxChangePlaceDTO convertChangePlace(JzdxReleaseEntity s) {
        if (Objects.isNull(s)) {
            return null;
        }
        JzdxChangePlaceDTO result = convertChangePlaceTo(s);

        List<String> areaList = new ArrayList<>();
        Actions.apply(StringUtils.isNotEmpty(s.getXzxdszs()), () -> areaList.add(s.getXzxdszs()));
        Actions.apply(StringUtils.isNotEmpty(s.getXzxdszds()), () -> areaList.add(s.getXzxdszds()));
        Actions.apply(StringUtils.isNotEmpty(s.getXzxdszxq()), () -> areaList.add(s.getXzxdszxq()));
        Actions.apply(StringUtils.isNotEmpty(s.getXzxdszd()), () -> areaList.add(s.getXzxdszd()));

        result.setAreaList(areaList);
        return result;
    }

    default JzdxReleaseEntity update(JzdxReleaseUpdateDTO s) {
        JzdxReleaseEntity entity = updateTo(s);
        convertPrison(entity, s.getPrison());
        convertDie(entity, s.getDie());
        convertChangePlace(entity, s.getChangePlace());
        return entity;
    }

    default JzdxReleaseResultDTO convert(JzdxReleaseEntity s) {
        if (Objects.isNull(s)) {
            return null;
        }
        JzdxReleaseResultDTO result = convertTo(s);
        result.setPrison(convertPrison(s));
        result.setDie(convertDie(s));
        result.setChangePlace(convertChangePlace(s));
        return result;
    }

    default JzdxReleaseEntity create(JzdxReleaseCreateDTO s, Long tenantId) {
        JzdxReleaseEntity entity = createTo(s);
        convertPrison(entity, s.getPrison());
        convertDie(entity, s.getDie());
        convertChangePlace(entity, s.getChangePlace());
        entity.setTenantId(tenantId);
        return entity;
    }

    default void convertPrison(JzdxReleaseEntity entity, JzdxPrisonDTO s) {
        Actions.apply(Objects.nonNull(s), () -> {
            entity.setSfsj(s.getSfsj());
            entity.setSjzxyy(s.getSjzxyy());
            entity.setSjzxlx(s.getSjzxlx());
            entity.setSjzxrq(s.getSjzxrq());
        });
    }

    default void convertDie(JzdxReleaseEntity entity, JzdxDieDTO s) {
        Actions.apply(Objects.nonNull(s), () -> {
            entity.setSwsj(s.getSwsj());
            entity.setSwyy(s.getSwyy());
            entity.setJtsy(s.getJtsy());
        });
    }

    default void convertChangePlace(JzdxReleaseEntity entity, JzdxChangePlaceDTO s) {
        Actions.apply(Objects.nonNull(s), () -> {
            entity.setJzdbgrq(s.getJzdbgrq());
            entity.setXjzddz(s.getXjzddz());
            additionalArea(entity, s.getAreaList());
        });
    }

    default JzdxReleaseCreateDTO changePlaceCreate(String code, String releaseType) {
        JzdxReleaseCreateDTO release = new JzdxReleaseCreateDTO();
        release.setRybh(code);
        release.setSfsjcr(SecurityUtils.getUser().getUsername());
        release.setSfsjcsj(LocalDateTime.now());
        release.setJjlx(releaseType);
        release.setJjrq(LocalDate.now());
        return release;
    }

    default void additionalArea(JzdxReleaseEntity s, List<String> areaList) {
        if (Objects.isNull(s) || CollectionUtils.isEmpty(areaList)) {
            return;
        }
        s.setXzxdszs(areaList.get(SupervisionBusinessConstants.AREA_PROVINCE_INDEX));
        s.setXzxdszds(areaList.get(SupervisionBusinessConstants.AREA_CITY_INDEX));

        if (areaList.size() >= SupervisionBusinessConstants.AREA_COUNTY_ARRAY_LENGTH) {
            s.setXzxdszxq(areaList.get(SupervisionBusinessConstants.AREA_COUNTY_INDEX));
        } else {
            s.setXzxdszd(null);
        }

        if (areaList.size() >= SupervisionBusinessConstants.AREA_VILLAGE_ARRAY_LENGTH) {
            s.setXzxdszxq(areaList.get(SupervisionBusinessConstants.AREA_VILLAGE_INDEX));
        } else {
            s.setXzxdszd(null);
        }
    }

}
